/**
 * 
 */
package engine;

import interfaces.IMeasure;

import java.util.ArrayList;

import massDistribution.MeasuredAttribute;
import core.SourceOfEvidence;

/**
 * @author  Elisa Costante
 */
public abstract class Service extends SourceOfEvidence implements Cloneable {

	private String url;
	private String identifierName;
	protected ArrayList<Service> monitoredServices;

	/**
	 * @param name
	 */
	public Service(String identifierName) {
		super(identifierName);
		this.identifierName = identifierName;
	}

	/**
	 * @return
	 * @uml.property  name="url"
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * @param url
	 * @uml.property  name="url"
	 */
	public void setUrl(String url) {
		this.url = url;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see interfaces.Iservice#getName()
	 */
	/**
	 * @return
	 * @uml.property  name="identifierName"
	 */
	@Override
	public String getIdentifierName() {

		return this.identifierName;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see interfaces.Iservice#setName(java.lang.String)
	 */
	/**
	 * @param name
	 * @uml.property  name="identifierName"
	 */
	@Override
	public void setIdentifierName(String name) {
		this.identifierName = name;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Service [name=" + this.identifierName + "]";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime
				* result
				+ ((this.identifierName == null) ? 0 : this.identifierName
						.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Service other = (Service) obj;
		if (this.identifierName == null) {
			if (other.identifierName != null)
				return false;
		} else if (!this.identifierName.equals(other.identifierName))
			return false;
		return true;
	}

	/**
	 * @return
	 * @uml.property  name="monitoredServices"
	 */
	public ArrayList<Service> getMonitoredServices() {
		return monitoredServices;
	}

	/**
	 * @param monitoredServices
	 * @uml.property  name="monitoredServices"
	 */
	public void setMonitoredServices(ArrayList<Service> monitoredServices) {
		this.monitoredServices = monitoredServices;
	}

	public void addMonitoredService(Service service) {
		if (monitoredServices == null)
			monitoredServices = new ArrayList<Service>();
		monitoredServices.add(service);
	}

	/**
	 * Allocate a <code>monitoredServices</code> size to the <code>size</code>
	 * value an all null initial elements;
	 * 
	 * @param size
	 */
	public void setMonitoredServicesSize(int size) {
		monitoredServices = new ArrayList<Service>(size);
		for (int i = 0; i < size; i++) {
			monitoredServices.add(null);
		}

	}

	/**
	 * Add the service in the i position of the monitoredServices list. If the
	 * list is empty a new one with i+1 initial capacity is cereated. If
	 * <code>i</code> is greater than the <code>monitoredServices</code> size
	 * then a new list with i+1 initial capacity and with the previus elements
	 * in the same position as before is created.
	 * 
	 * @param service
	 * @param i
	 */
	public void addMonitoredService(Service service, int i) {
		if (monitoredServices == null) {
			monitoredServices = new ArrayList<Service>();
		}
		if (i >= monitoredServices.size()) {
			ArrayList<Service> tmp = new ArrayList<Service>(i + 1);
			for (int j = 0; j < monitoredServices.size(); j++) {
				tmp.add(j, monitoredServices.get(j));
			}
			monitoredServices = tmp;
		}
		monitoredServices.add(i, service);
	}

	/**
	 * Returns the element in the i position or null if the size of
	 * <code>monitoredServices</code> is less than i.
	 * 
	 * @param i
	 * @return
	 */
	public Service getMonitoredService(int i) {
		if (monitoredServices.size() < i) {
			return monitoredServices.get(i);
		} else {
			return null;
		}
	}

	/**
	 * Returns the service with the <code>name</code> identifier or null if no
	 * match is found.
	 * 
	 * @param name
	 * @return
	 */
	public Service getMonitoredService(String name) {
		if (monitoredServices != null) {
			for (int j = 0; j < monitoredServices.size(); j++) {
				String identifier = monitoredServices.get(j)
						.getIdentifierName();
				if (identifier.equalsIgnoreCase(name))
					return monitoredServices.get(j);
			}
		}
		return null;
	}

	@Override
	public abstract Object clone() throws CloneNotSupportedException;

	/**
	 * Read the value of the attribute collected from the Source for the
	 * service. If the source does not collect data from the
	 * <code>attribute</code> the value {@link IMeasure}.
	 * <code>NOT_MEASURED</code> will be returned. If the source collect any
	 * info about the service return null:
	 * 
	 * @param service
	 * @param attribute
	 * @return the {@link IMeasure} for the {@link MeasuredAttribute} if it is
	 *         measured or null if the service is not monitored at all.
	 */
	public abstract IMeasure readMeasure(Service service,
			MeasuredAttribute attribute);
}
